/*
 Exercise 6.2addendum
 Display and hold random numbers to 
 a 4 x 7-segment module
 http://tronixstuff.com/tutorials > Chapter Six addendum
 */

int latchpin = 9; // connect to pin 12 on the '595
int clockpin = 7; // connect to pin 11 on the '595
int datapin = 10; // connect to pin 14 on the '595
int sensor = 0; // value from analogue sensor. 
int lhd = 0;
int mhd = 0;
int mhd2 = 0;
int rhd = 0;
float a = 0;
int b = 0;
int c= 0;
float d = 0;
int leadingzero = 1; // 0 for no leading zereos, 1 for leading zeroes


int segdisp[10] = {
  123, 72,103,79,92,31,63,74,127,95}; // base 10 equivalents for digits 0~9
int posdisp[4] = {
  1,2,4,8}; // base 10 equivalents to close cathodes on display 0~3 on module

void setup()
{
  pinMode(latchpin, OUTPUT);
  pinMode(clockpin, OUTPUT);
  pinMode(datapin, OUTPUT);
  randomSeed(analogRead(0));
}


void cleardisplay()
// turns off all segments of all digits 
{
  for (int aa=0; aa<4; aa++)
  {
    digitalWrite(latchpin, LOW);
    shiftOut(datapin, clockpin, MSBFIRST, posdisp[aa]); // sets the digit to address
    shiftOut(datapin, clockpin, MSBFIRST, 0); // clears the digit
    digitalWrite(latchpin, HIGH);
  }
}

void digitdisplay(int digit, int location)
// displays "digit" on display "location" 0~3
{
  digitalWrite(latchpin, LOW);
  shiftOut(datapin, clockpin, MSBFIRST, posdisp[location]); // sets the digit to address
  shiftOut(datapin, clockpin, MSBFIRST, segdisp[digit]); // clears the digit
  digitalWrite(latchpin, HIGH);
}

void onedigitnumber(int subject)
// displays a one-digit number on the display module with leading zeroes
{
  cleardisplay();
  if (leadingzero==1)
  {
    digitdisplay(0,0);
    digitdisplay(0,1);
    digitdisplay(0,2);
  }
  digitdisplay(subject,3);
}

void twodigitnumber(int subject)
// displays a two-digit number on the display module with leading zeroes
{
  cleardisplay();
  rhd = subject % 10;
  a = subject/10;
  lhd = int(a);
  if (leadingzero==1)
  {
    digitdisplay(0,0);
    digitdisplay(0,1);
  }
  digitdisplay(lhd,2);
  digitdisplay(rhd,3);
}

void threedigitnumber(int subject)
// displays a three-digit number on the display module with leading zeroes
{
  cleardisplay();
  a = subject/100;
  lhd = int(a);
  a = subject/10;
  b = int(a); 
  mhd = b % 10;
  b=subject%100;
  rhd=b%10;
  if (leadingzero==1)
  {
    digitdisplay(0,0);
  }
  digitdisplay(lhd,1);
  digitdisplay(mhd,2);
  digitdisplay(rhd,3);
}

void fourdigitnumber(int subject)
// displays a four-digit number on the display module with leading zeros
{
  cleardisplay();
  a = subject/1000;
  lhd = int(a);
  b=lhd*1000;
  c=subject-b;
  a = c/100;
  mhd = int(a);
  a = c/10;
  b = int(a); 
  mhd2 = b % 10;
  b=subject%1000;
  c=b%100;
  rhd=c%10;
  digitdisplay(lhd,0);
  digitdisplay(mhd,1);
  digitdisplay(mhd2,2);
  digitdisplay(rhd,3);
}

void displaynumber(int rawnumber, int cycles)
// takes an integer and displays it on our 4-digit LED display module and HOLDS it on the display for 'cycles' number of cycles
{
  for (int q=1; q<=cycles; q++)
  {

    if (rawnumber>=0 && rawnumber<10)
    {
      onedigitnumber(rawnumber);
    } 
    else if (rawnumber>=10 && rawnumber<100)
    {
      twodigitnumber(rawnumber);
    } 
    else if (rawnumber>=100 && rawnumber<1000)
    {
      threedigitnumber(rawnumber);
    } 
    else if (rawnumber>=1000)
    {
      fourdigitnumber(rawnumber);
    }
  }
}

void loop()
{
  sensor=random(10000);
  displaynumber(4, 500); // 100 is the number of times to send the number to the display module. 
}






















